package com.ylyq.ylxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylyq.ylxx.entity.YlyqAreaOceangine;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.GetBalanceRequest;
import com.ylyq.ylxx.model.bean.OceanengineGetBalance;
import com.ylyq.ylxx.model.request.GetYlyqAreaRequest;
import com.ylyq.ylxx.service.IYlyqAreaOceangineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 省市县、对应的巨量city id 前端控制器
 * </p>
 *
 * @author xiaweichao
 * @since 2020-09-14
 */
@RestController
@RequestMapping("/ylyqAreaOceangine")
@Api(description = "省市县、对应的巨量city id接口")
public class YlyqAreaOceangineController {
    @Autowired
    private IYlyqAreaOceangineService iYlyqAreaOceangineService;

    @PostMapping("/getArea")
    @ApiOperation(value = "查询省市县列表", response = YlyqAreaOceangine.class)
    public ResponseData getArea(@RequestBody GetYlyqAreaRequest getYlyqAreaRequest) {

        QueryWrapper<YlyqAreaOceangine> ylyqAreaOceangineQueryWrapper = new QueryWrapper<>();
        ylyqAreaOceangineQueryWrapper.select("id", "city_leave", "parent_id", "city_name");
        if (getYlyqAreaRequest == null)
            return new ResponseData(ResponseData.Result.SUCCESS, iYlyqAreaOceangineService.list(ylyqAreaOceangineQueryWrapper));

        if (getYlyqAreaRequest.getCityNameList() != null && getYlyqAreaRequest.getCityNameList().length > 0)
            ylyqAreaOceangineQueryWrapper.in("city_name", getYlyqAreaRequest.getCityNameList());
        else if (StringUtils.isNotBlank(getYlyqAreaRequest.getCityName()))
            ylyqAreaOceangineQueryWrapper.like("city_name", getYlyqAreaRequest.getCityName());
        if (StringUtils.isNotBlank(getYlyqAreaRequest.getParentId()))
            ylyqAreaOceangineQueryWrapper.eq("parent_id", getYlyqAreaRequest.getParentId());
        if (StringUtils.isNotBlank(getYlyqAreaRequest.getId()))
            ylyqAreaOceangineQueryWrapper.eq("id", getYlyqAreaRequest.getId());
        if (getYlyqAreaRequest.getCityLeave() != null && getYlyqAreaRequest.getCityLeave().length > 0)
            ylyqAreaOceangineQueryWrapper.in("city_leave", getYlyqAreaRequest.getCityLeave());

        return new ResponseData(ResponseData.Result.SUCCESS, iYlyqAreaOceangineService.list(ylyqAreaOceangineQueryWrapper));
    }
    @PostMapping("/getArea1")
    @ApiOperation(value = "查询省市县列表", response = YlyqAreaOceangine.class)
    public ResponseData getArea1(@RequestParam String str) {

        Map<String,Object> re=new HashMap<>();

        Set<YlyqAreaOceangine> result=new HashSet<>();
        Set<String> resultError=new HashSet<>();


        QueryWrapper<YlyqAreaOceangine> ylyqAreaOceangineQueryWrapper = new QueryWrapper<>();
        ylyqAreaOceangineQueryWrapper.select("id", "city_leave", "parent_id", "city_name");

        String replace = str.replace("省", "").replace("市", "");
        String[] split = replace.split(" ");
        GetYlyqAreaRequest getYlyqAreaRequest=new GetYlyqAreaRequest();
        getYlyqAreaRequest.setCityNameList(split);

        if (getYlyqAreaRequest == null)
            return new ResponseData(ResponseData.Result.SUCCESS, iYlyqAreaOceangineService.list(ylyqAreaOceangineQueryWrapper));

        if (getYlyqAreaRequest.getCityNameList() != null && getYlyqAreaRequest.getCityNameList().length > 0)
            ylyqAreaOceangineQueryWrapper.in("city_name", getYlyqAreaRequest.getCityNameList());
        else if (StringUtils.isNotBlank(getYlyqAreaRequest.getCityName()))
            ylyqAreaOceangineQueryWrapper.like("city_name", getYlyqAreaRequest.getCityName());
        if (StringUtils.isNotBlank(getYlyqAreaRequest.getParentId()))
            ylyqAreaOceangineQueryWrapper.eq("parent_id", getYlyqAreaRequest.getParentId());
        if (StringUtils.isNotBlank(getYlyqAreaRequest.getId()))
            ylyqAreaOceangineQueryWrapper.eq("id", getYlyqAreaRequest.getId());
        if (getYlyqAreaRequest.getCityLeave() != null && getYlyqAreaRequest.getCityLeave().length > 0)
            ylyqAreaOceangineQueryWrapper.in("city_leave", getYlyqAreaRequest.getCityLeave());
        List<YlyqAreaOceangine> ylyqAreaOceangines = iYlyqAreaOceangineService.list(ylyqAreaOceangineQueryWrapper);
        String[] first = str.split(" ");
        for (String temp:first) {
            boolean flag=false;
            for (YlyqAreaOceangine ylyqArea:ylyqAreaOceangines) {
                if(temp.endsWith("省")&&temp.replace("省","").equals(ylyqArea.getCityName())&&ylyqArea.getCityLeave()==1){
                    result.add(ylyqArea);
                    flag=true;
                }else if(temp.endsWith("市")&&temp.replace("市","").equals(ylyqArea.getCityName())&&ylyqArea.getCityLeave()==2){
                    result.add(ylyqArea);
                    flag=true;
                }else if(temp.equals(ylyqArea.getCityName())&&(ylyqArea.getCityLeave()==1||ylyqArea.getCityLeave()==2)){
                    result.add(ylyqArea);
                    flag=true;
                }
            }
            if(!flag){
                resultError.add(temp);
            }
        }
        re.put("success",result);
        re.put("fail",resultError);

        return new ResponseData(ResponseData.Result.SUCCESS, re);
    }
}
